using UnityEngine;
using System.Collections;

public class ColorFactory : MonoBehaviour 
{
	public ColorPanel colorPanel;
	private ArrayList colorsList = new ArrayList();
	private Color color;
	public Transform cubePrefab;
	public Transform trianglePrefab;
	private Transform newObject;
	private bool occupied;
	public static int stage = 1;
	
	public Color setColor(Color32 color)
	{
		if(!colorsList.Contains(color))
		{
			Color temp = color;
		
			colorsList.Add(temp);
		}
		
		return switchColors();
	}
	
	public Color removeColor(Color32 color)
	{
		Color temp = color;
		colorsList.Remove(temp);
		
		return switchColors();
	}

	public Color switchColors()
	{
		this.color = Color.black;
		
		foreach (Color c in colorsList) 
		{
			color += c;
		}

		return color;
	}
	
	public void createCube()
	{
		if(occupied)
		{
			Destroy(newObject.gameObject);
		}
		
		if(stage == 1)
		{
			newObject = (Transform)Instantiate(cubePrefab, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);
			newObject.renderer.material.color = new Color(color.r,color.g,color.b,1);
		}
		else
		{
			newObject = (Transform)Instantiate(trianglePrefab, new Vector3(transform.position.x, transform.position.y + 2, transform.position.z), Quaternion.identity);
			newObject.FindChild("Triangulo").renderer.material.color = new Color(color.r,color.g,color.b,1);
		}

		occupied = true;
		colorsList.Clear();
		colorPanel.renderer.materials[1].color = switchColors();
	}
	
	void OnCollisionExit(Collision other)
	{
		if(other.gameObject.tag == "Objeto")
		{
			occupied = false;
		}
	}
}